package kr.ac.skhu.m.controller.neuti.community;


import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.ac.skhu.m.value.FoodMenuValue;


import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;


public class FoodMenuC extends AbstractController {
	
	private String successView;
	
	public void setSuccessView(String successView) {
		this.successView = successView;
	}

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		ModelAndView mav = new ModelAndView(successView);
		
		String urlStr = "http://www.skhu.ac.kr/uni_zelkova/uni_zelkova_4_3_list.aspx";
		URL url = new URL(urlStr);
		Source source = new Source(url);
		
		source.fullSequentialParse();
		
		List<String> menuTitle = new ArrayList<String>();
		List<Element> left15 = source.getAllElementsByClass("left15");
		int left15Size = left15.size();
		
		List<FoodMenuValue> foodMenuList = new ArrayList<FoodMenuValue>();
		
		for(int index=0; index<left15Size; index++){
			Element link = left15.get(index).getAllElements(HTMLElementName.A).get(0);
			String title = link.getTextExtractor().toString();
			menuTitle.add(title);
			
			String href = link.getAttributeValue("href");
			urlStr = "http://www.skhu.ac.kr/uni_zelkova/" + href;
			
			url = new URL(urlStr);
			source = new Source(url);
			
			source.fullSequentialParse();
			
			List<Element> tr = source.getAllElementsByClass("cont_c").get(0).getAllElements(HTMLElementName.TR); 
			
			for(int i=0; i<5; i++){
				
				FoodMenuValue foodMenuData = new FoodMenuValue();
				
				String dayOfTheWeek = tr.get(0).getAllElements(HTMLElementName.TH).get(1+i).getTextExtractor().toString();
				String date = tr.get(1).getAllElements(HTMLElementName.TH).get(2+i).getTextExtractor().toString();
				String lunchA = tr.get(2).getAllElements(HTMLElementName.TD).get(0+i).getTextExtractor().toString();
				String lunchB = tr.get(4).getAllElements(HTMLElementName.TD).get(0+i).getTextExtractor().toString();
				String lunchC = tr.get(6).getAllElements(HTMLElementName.TD).get(0+i).getTextExtractor().toString();
				String dinner = tr.get(8).getAllElements(HTMLElementName.TD).get(0+i).getTextExtractor().toString();	
				
				String lunchATemp = "";
				String lunchBTemp = "";
				String lunchCTemp = "";
				String dinnerTemp = "";
				
				String[] lunchAArr = lunchA.split(" ");
				String[] lunchBArr = lunchB.split(" ");
				//String[] lunchCArr = lunchC.split(" ");
				String[] dinnerArr = dinner.split(" ");
				
				for(int o=0; o<lunchAArr.length; o++){
					if(lunchAArr[o].length() == 1){
						if(o < dinnerArr.length-1){
							lunchATemp += lunchAArr[o] + lunchAArr[o+1] + " ";
							++o;
						}
					}else{
						lunchATemp += lunchAArr[o] + " ";
					}
				}
				
				for(int o=0; o<lunchBArr.length; o++){
					if(lunchBArr[o].length() == 1){
						if(o < dinnerArr.length-1){
							lunchBTemp += lunchBArr[o] + lunchBArr[o+1] + " ";
							++o;
						}
					}else{
						lunchBTemp += lunchBArr[o] + " ";
					}
				}
				/*
				for(int o=0; o<lunchCArr.length; o++){
					if(lunchCArr[o].length() == 1){
						lunchCTemp += lunchCArr[o] + lunchCArr[o+1] + " ";
						++o;
					}else{
						lunchCTemp += lunchCArr[o] + " ";
					}
				}
				*/
				for(int o=0; o<dinnerArr.length; o++){
					if(dinnerArr[o].length() == 1){
						if(o < dinnerArr.length-1){	
							dinnerTemp += dinnerArr[o] + dinnerArr[o+1] + " ";
							++o;
						}
					}else{
						dinnerTemp += dinnerArr[o] + " ";
					}
				}
				
				lunchATemp = lunchATemp.replace(" ", "<br/>");
				lunchBTemp = lunchBTemp.replace(" ", "<br/>");
				lunchCTemp = lunchCTemp.replace(" ", "<br/>");
				dinnerTemp = dinnerTemp.replace(" ", "<br/>");
				
				foodMenuData.setDayOfTheWeek(dayOfTheWeek);
				foodMenuData.setDate(date);
				foodMenuData.setLunchA(lunchATemp);
				foodMenuData.setLunchB(lunchBTemp); 
				foodMenuData.setLunchC(lunchCTemp);
				foodMenuData.setDinner(dinnerTemp);
				foodMenuData.setOrder(String.valueOf(index));
				
				foodMenuList.add(foodMenuData);
			}
		}
		
		
		mav.addObject("foodMenuList", foodMenuList);
		mav.addObject("menuTitle", menuTitle);
		
		Calendar calendar = Calendar.getInstance();
		String today = calendar.get(Calendar.YEAR) + "-" + calendar.get(Calendar.MONTH) + "-" + calendar.get(Calendar.DATE);
		
		mav.addObject("today", today);
		
		return mav;
	}
	
}
